<template>
  <div class="tagList">
    <el-tag
      v-for="(item, index) in tags"
      :key="item.path"
      :closable="item.name !== 'home'"
      :effect="$route.name === item.name ? 'dark' : 'plain'"
      @click="changeMenu(item)"
      @close="delMenu(item, index)"
      size="small"
    >
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  computed: {
    ...mapState({
      tags: (state) => state.tab.tabList,
    }),
  },
  methods: {
    ...mapMutations(["delBread"]),
    // 点击tag跳转的功能
    changeMenu(item) {
      this.$router.push({ name: item.name });
    },
    // 点击tag删除的功能
    delMenu(item, index) {
      // 删除数据
      // this.$store.commit("delBread", item);
      this.delBread(item);
      const length = this.tags.length; //3 1=>2 1
      // 删除之后的跳转逻辑
      // 如果点击的不是激活的tag，不跳转
      if (item.name !== this.$route.name) {
        return;
      } else {
        // 删除的激活的tag是最后一项
        if (index === length) {
          this.$router.push({ name: this.tags[index - 1].name });
        }
        // 删除的激活的tag不是最后一项
        if (index !== length) {
          this.$router.push({ name: this.tags[index].name });
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.tagList {
  padding: 20px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>
